<template>
	<view class="content">
		<view class="titlebg">
			<view class="title_bg">
				<label class="title">消息标题</label>
			</view>
			<view class="inputbg">
				<input type="text" placeholder="请输入消息标题" v-model="msgtitle" />
			</view>
		</view>
		<view class="subtitlebg">
			<view class="title_bg">
				<label class="title">消息副标题</label>
			</view>
			<view class="inputbg">
				<input type="text" placeholder="请输入消息标题" v-model="msgsubtitle" />
			</view>
		</view>
		<view class="contentbg">
			<view class="title_bg">
				<label class="title">消息内容</label>
			</view>
			<view class="inputbg">
				<textarea class="msgtext" cols="100" rows="50" v-model="msgcontent"></textarea>
			</view>
		</view>
		<view>
			<view class="buttons">
				<button class="subBtn" type="primary" @click="subMsgChange">提交</button>
			</view>
			<view class="buttons">
				<button class="subBtn" type="default" @click="deleteMessage">删除</button>
			</view>
		</view>
		<view>
			<!-- 提示窗示例 -->
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog :showClose="showClose" :type="msgType" cancelText="关闭" confirmText="同意" title="通知"
					:content="alertTitle" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import config from '../../common/config.js';
	export default {
		onLoad(option) {
			let item = JSON.parse(decodeURIComponent(option.item));
			console.log(item);
			this.msgobj = item;
			this.msgtitle = item.message_title;
			this.msgsubtitle = item.message_sub_title;
			this.msgcontent = item.message_content;
			uni.setNavigationBarTitle({
				title: item.message_title
			});
		},
		data() {
			return {
				msgobj: {},
				msgtitle: "",
				msgsubtitle: "",
				msgcontent: "",
				showClose: true,
				alertType:'',
				alertTitle:""
			}
		},
		methods: {
			dialogToggle(type,alertType) {
				this.alertType = alertType;
				this.msgType = type;
				this.$refs.alertDialog.open();
			},
			dialogClose() {
				console.log('点击关闭')
			},
			dialogConfirm() {
				console.log('点击确认')
				if(this.alertType == "change"){
					this.subMessageChange();
				}
				if(this.alertType == "delete"){
					this.msgDelete();
				}
			},
			subMsgChange(){
				this.dialogToggle("warning","change");
				this.alertTitle = "是否修改消息";
			},
			subMessageChange() {
				
				let editURL = config.baseURL + "message/editmsg";
				uni.showLoading({
					title:"等待中..."
				});
				self = this;
				uni.request({
					url: editURL,
					method: 'POST',
					header: {
						"Authorization": config.getToken()
					},
					data: {
						"message_id": self.msgobj.id,
						"msgtitle": self.msgtitle,
						"msgsubtitle": self.msgsubtitle,
						"msgcontent": self.msgcontent
					},
					success: (res) => {
						uni.hideLoading();
						console.log(res.data);
						if (res.data.isok == 1) {
							uni.showToast({
								title: "消息修改成功",
								icon: "success"
							});
							uni.navigateBack();
						} else {
							uni.showToast({
								title: "消息修改失败",
								icon: "error"
							});
						}
					},
					fail: (res) => {
						console.log(res.errMsg.error_code);
						uni.hideLoading();
						if (res.errMsg.error_code == 401) {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}
						uni.showToast({
							title: "消息修改失败",
							icon: "error"
						})
					}
				})
			},
			deleteMessage(){
				this.dialogToggle("warning","delete");
				this.alertTitle = "消息删除后将无法找回，是否删除";
			},
			msgDelete(){
				let delMsgURL = config.baseURL+"message/delmsg";
				uni.showLoading({
					title:"等待中..."
				});
				self = this;
				uni.request({
					url:delMsgURL,
					method:'POST',
					header: {
						"Authorization": config.getToken()
					},
					data:{
						"message_id":self.msgobj.id
					},
					success: (res) => {
						uni.hideLoading();
						console.log(res.data);
						if (res.data.isok == 1) {
							uni.showToast({
								title: "消息删除成功",
								icon: "success"
							});
							uni.navigateBack();
						} else {
							uni.showToast({
								title: "消息修改失败",
								icon: "error"
							});
						}
					},
					fail: (res) => {
						console.log(res.errMsg.error_code);
						uni.hideLoading();
						if (res.errMsg.error_code == 401) {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}
						uni.showToast({
							title: "消息修改失败",
							icon: "error"
						})
					}
				})
			}
		}
	}
</script>

<style>
	.title_bg {
		width: 710rpx;
		margin: 20rpx;
	}

	.inputbg {
		width: 710rpx;
		margin: 20rpx;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		border-radius: 15rpx;
		border-width: 3rpx;
		border-style: solid;
		border-color: #7e7e7e;
	}

	.msgtext {
		width: 690rpx;
		margin: 10rpx;
	}

	input {
		width: 690rpx;
		margin: 10rpx;
	}

	.buttons {
		width: 710rpx;
		margin: 20rpx;
		display: flex;
		align-items: center;
		justify-items: center;
	}

	.subBtn {
		width: 650rpx;
	}
</style>